<?php
declare (strict_types=1);

namespace app\api\controller;

use think\Exception;
use think\response\Json;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use app\api\service\passport\Login as LoginService;
use app\api\model\User as UserModel;
use app\api\model\UserOauth as UserOauthModel;
// use app\api\service\{user\Oauth as OauthService, passport\Party as PartyService};
use app\common\enum\Client as ClientEnum;
use cores\exception\BaseException;
use tool\Captcha;
use app\api\service\passport\SmsCaptcha;

/**
 * 用户认证模块
 * Class Passport
 * @package app\api\controller
 */
class Passport extends Controller
{
    /**
     * 登录接口 (需提交手机号、短信验证码、第三方用户信息)
     * @return Json
     * @throws BaseException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(): Json
    {
        // 执行登录
        $LoginService = new LoginService;
        if (!$LoginService->login($this->postForm())) {
            return $this->renderError($LoginService->getError());
        }
        // 用户信息
        $userInfo = $LoginService->getUserInfo();
        $model = new UserModel;
        return $this->renderSuccess([
            'token' => $model->login((int)$userInfo['user_id'])
        ], '登录成功');
    }

    /**
     * 快捷登录: 微信小程序授权手机号登录
     * @return Json
     * @throws BaseException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function loginMpWxMobile(): Json
    {
        $form = $this->postForm();
        // 获取微信小程序登录态(session)
        $LoginService = new LoginService;
        $result = $LoginService->wxCode2Phone($form['code']);
        return $this->renderSuccess([
            'mobile' => $result['purePhoneNumber']
        ]);
    }

    public function loginMpWx()
    {
        $LoginService = new LoginService;
        $result = $LoginService->wxCode2Session(input('post.code'));
        $userId = UserOauthModel::getUserIdByOauthId($result['openid'], ClientEnum::MP_WEIXIN);
        if($userId != null){
            $model = new UserModel;
            UserModel::update([
                'last_login_time' => time(),
                'update_time' => time()
            ],['user_id' => $userId]);
            return $this->renderSuccess([
                'is_register' => true,
                'token' => $model->login((int)$userId)
            ], '登录成功');
        }else{
            return $this->renderSuccess([
                'is_register' => false,
                'openid' => $result['openid']
            ]);
        }
    }

    public function verify()
    {
        $captcha = new Captcha;
        return $captcha->create($this->request->get('mobile'));
    }
    public function sendsms()
    {
        $mobile = $this->request->post('mobile');
        $captcha = new Captcha;
        $captcha->check($this->request->post('captchaCode'), $mobile) OR throwError('图形验证码错误');
        $smsCaptcha = new SmsCaptcha;
        $smsCaptcha->sendCaptcha($mobile);
        return $this->renderSuccess();
    }
}